package find.prj.prog;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.example.androidrssreader.RssItem;

public class ConsoleTest {
	
	public static void main(String[] args) {
		Document xmldoc = null;
		try {
			xmldoc = getXmlDocument("http://www.pravda.com.ua/rss/");
		} catch (MalformedURLException e) {
			System.out.println("wromg URL");
		} catch (SAXException e) {
			System.out.println("cant parse XML");
		}
		if (xmldoc == null)
			System.out.println("blank XML");
		XPath xPath = XPathFactory.newInstance().newXPath();
		NodeList nodeList = null;
		try {
			nodeList = (NodeList) xPath.evaluate("/rss", xmldoc, XPathConstants.NODESET);
		} catch (XPathExpressionException e1) {
		}
		if (nodeList.getLength() > 0) {
			NodeList items = null;
			try {
				items = (NodeList) xPath.evaluate("/rss/channel/item", xmldoc, XPathConstants.NODESET);
			} catch (XPathExpressionException e) {
			}
			System.out.println(items.getLength());
			
			
			
		}
	}
		

		
		
		
		
		/*		if (xmldoc.getDocumentElement().getNodeName().toLowerCase().trim() == "rss") {
			NodeList items = xmldoc.getElementsByTagName("item");
			for (int i=0; i< items.getLength(); i++) {
				Node item = items.item(i);
				if (item.getNodeType() == Node.ELEMENT_NODE) {
					RssItem rssItem = new RssItem();
					rssItem.title = getRssItemChildString(item, "title");
					rssItem.description = getRssItemChildString(item, "description");
					rssItem.fullText = getRssItemChildString(item, "fulltext");
					try {
						rssItem.link = new URL(getRssItemChildString(item, "link"));
					} catch (MalformedURLException e) {
						rssItem.link = null;
					}
					try {
						rssItem.pdaLink = new URL(getRssItemChildString(item, "pdalink"));
					} catch (MalformedURLException e) {
						rssItem.pdaLink = null;
					}
					rssItem.pubDate = getRssItemChildString(item, "pubdate");
					this.publishProgress(rssItem);
				}
			}
			this.taskState = RSS_SUCCESSFULLY_READ;
			this.publishProgress((RssItem)null);
		}
		else
			this.taskState = XML_DONT_CONTAIN_RSS;
			this.publishProgress((RssItem)null);*/

	private static Document getXmlDocument (String urlString) throws SAXException, MalformedURLException {
		HttpURLConnection httpCon = null;
		Document xmldoc = null;
		try {
			URL url = new URL(urlString);
			httpCon = (HttpURLConnection)url.openConnection();
			httpCon.setRequestMethod("GET");
			httpCon.setRequestProperty("Content-Type", "application/rss+xml");
			httpCon.getResponseMessage();
			
			System.out.println(httpCon.getContentEncoding() + "\n" + httpCon.getResponseMessage());
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			xmldoc = dBuilder.parse(new InputSource(new BufferedReader(new InputStreamReader(httpCon.getInputStream()))));
		}
		catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} 
		finally {
			if(httpCon != null)
				httpCon.disconnect(); 
	    }
		System.out.println(xmldoc.getXmlEncoding());
		return xmldoc; 
	}

}
